#include "KillCounter.h"
#include "MessageSystem.h"
#include "Message.h"
#include "..\Managers\StateManager.h"
#include "..\States\WinState.h"
#include "..\Managers\RenderManager.h"
#include "..\Generic\Convert.h"
#include "..\Define.h"
#include "..\Components\ComponentVisitor.h"

KillCounter::KillCounter(int p_count) : m_target_kills(p_count), m_elapsed_kills(0)
{
	float btnScale = 0.4f;
	MessageSystem::getSingleton().registerSubject<KillCounter>("kill", this, &KillCounter::killMsg);
	m_text = RenderManager::getSingleton().getRenderEngine()->createText(2, "mm_font", "Kills left:");
	m_counter = RenderManager::getSingleton().getRenderEngine()->createText(2, "mm_font", numberToString(m_target_kills));
	m_text->setColor(vn::Color::red);
	m_counter->setColor(vn::Color::red);
	m_text->setScale(btnScale, btnScale);
	m_counter->setScale(btnScale, btnScale);
	m_text->setPosition(10.0f, VSH - 30.0f);
	m_counter->setPosition(150.0f, VSH - 30.0f);
}


KillCounter::~KillCounter(void)
{
	MessageSystem::getSingleton().unregister("kill", this);
	RenderManager::getSingleton().getRenderEngine()->destroyText(m_text);
	RenderManager::getSingleton().getRenderEngine()->destroyText(m_counter);
}

void KillCounter::killMsg(const std::string& p_id, Message* p_message)
{
	m_elapsed_kills++;
	m_counter->setString(numberToString(m_target_kills - m_elapsed_kills));
	if (m_elapsed_kills >= m_target_kills)
	{
		StateManager::getSingleton().setState(new WinState);
	}
}

void KillCounter::acceptVisitor(ComponentVisitor* p_componentVisitor)
{
	p_componentVisitor->visitKillCounter(this);
}

void KillCounter::setCurrentKillCount(int p_killCount)
{
	m_elapsed_kills = p_killCount;
	m_counter->setString(numberToString(m_target_kills - m_elapsed_kills));
	if (m_elapsed_kills >= m_target_kills)
	{
		StateManager::getSingleton().setState(new WinState);
	}
}